package com.jiguang.Electricvehicle;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;


/**
 * Created by YoKeyword on 16/6/3.
 */
public class BottomBarTab extends FrameLayout {
    Object selectedIcon;
    Object unSelectIcon;
    private ImageView mIcon;
    private TextView mTvTitle;
    private Context mContext;
    private int mTabPosition = -1;

    private TextView mTvDot;

    public BottomBarTab(Context context, Object selectedIcon, Object unSelectIcon, CharSequence title) {
        this(context, null, selectedIcon, unSelectIcon, title);
    }

    public BottomBarTab(Context context, AttributeSet attrs, Object selectedIcon, Object unSelectIcon, CharSequence title) {
        this(context, attrs, 0, selectedIcon, unSelectIcon, title);
    }

    public BottomBarTab(Context context, AttributeSet attrs, int defStyleAttr, Object selectedIcon, Object unSelectIcon, CharSequence title) {
        super(context, attrs, defStyleAttr);
        init(context, selectedIcon, unSelectIcon, title);
    }

    /**
     * 5.0版本
     */
    private void init(Context context, Object selectedIcon, Object unselectIcon, CharSequence title) {
        mContext = context;
        this.selectedIcon = selectedIcon;
        this.unSelectIcon = unselectIcon;
        LinearLayout lLContainer = new LinearLayout(context);
        lLContainer.setOrientation(LinearLayout.VERTICAL);
        lLContainer.setGravity(Gravity.CENTER);
        LayoutParams paramsContainer = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        paramsContainer.gravity = Gravity.CENTER;
        lLContainer.setLayoutParams(paramsContainer);
        lLContainer.setTag("ll_item");

        mIcon = new ImageView(context);
        int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 27, getResources().getDisplayMetrics());
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(size, size);
        if (unselectIcon instanceof String) {
            String url = (String) unselectIcon;
            ImageLoader.load(context, url, mIcon);
        } else {
            mIcon.setImageResource((Integer) unselectIcon);
        }
        mIcon.setLayoutParams(params);
        lLContainer.addView(mIcon);

        mTvTitle = new TextView(context);
        mTvTitle.setText(title);
        LinearLayout.LayoutParams paramsTv = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        paramsTv.topMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
        mTvTitle.setTextSize(10);
        mTvTitle.setTextColor(ContextCompat.getColor(context, R.color.text_content));
        mTvTitle.setLayoutParams(paramsTv);
        lLContainer.addView(mTvTitle);

        addView(lLContainer);

        int min = dip2px(context, 5);
        mTvDot = new TextView(context);
        mTvDot.setBackgroundResource(R.drawable.mine_dot_shape);
        mTvDot.setMinWidth(min);
        mTvDot.setTextColor(Color.WHITE);
        mTvDot.setGravity(Gravity.CENTER);
        LayoutParams tvUnReadParams = new LayoutParams(min, min);
        tvUnReadParams.gravity = Gravity.CENTER;
        tvUnReadParams.leftMargin = dip2px(context, 15);
        tvUnReadParams.bottomMargin = dip2px(context, 15);
        mTvDot.setLayoutParams(tvUnReadParams);
        mTvDot.setVisibility(GONE);

        addView(mTvDot);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            if (selectedIcon instanceof String) {//124*98

                String url = (String) selectedIcon;
                ImageLoader.load(mContext, url, mIcon);
            } else {
                mIcon.setImageResource((Integer) selectedIcon);
            }

            if (mTvTitle != null) {
                mTvTitle.setTextColor(ContextCompat.getColor(mContext, R.color.c4895FD));
            }
        } else {
            if (unSelectIcon instanceof String) {
                String url = (String) unSelectIcon;
                ImageLoader.load(mContext, url, mIcon);
            } else {
                mIcon.setImageResource((Integer) unSelectIcon);
            }
            if (mTvTitle != null) {
                mTvTitle.setTextColor(ContextCompat.getColor(mContext, R.color.c787C86));
            }
        }
    }

    public int getTabPosition() {
        return mTabPosition;
    }

    public void setTabPosition(int position) {
        mTabPosition = position;
        if (position == 0) {
            setSelected(true);
        }
    }

    public void changeImage(boolean selected, Object selectedIcon, Object unselectIcon) {
        mTvTitle.setVisibility(GONE);
        this.selectedIcon = selectedIcon;
        this.unSelectIcon = unselectIcon;
        setSelected(selected);
    }

    public void recoverImage(boolean selected, int checkResId, int uncheckResId) {
        mTvTitle.setVisibility(VISIBLE);
        this.selectedIcon = checkResId;
        this.unSelectIcon = uncheckResId;
        setSelected(selected);
    }

    public LinearLayout.LayoutParams setBadgeImage(LinearLayout.LayoutParams params) {
        mTvTitle.setVisibility(GONE);
        params.height = LayoutParams.MATCH_PARENT;
        params.width = params.height * 124 / 98;
        return params;
    }

    /**
     * 设置tab Icon
     *
     * @param resouce
     */
    public void setIcon(int resouce) {
        mIcon.setVisibility(VISIBLE);
        mIcon.setImageResource(resouce);
    }

    /**
     * 设置tab gif
     *
     * @param resouce
     */
    public void setIconGif(int resouce) {
        mIcon.setVisibility(VISIBLE);
        ImageLoader.loadGif(getContext(), resouce, mIcon);
    }

    /**
     * 显示红点
     */
    public void showDot() {
        mTvDot.setVisibility(VISIBLE);
    }

    /**
     * 隐藏红点
     */
    public void hidenDot() {
        mTvDot.setVisibility(GONE);
    }

    /**
     * 设置未读数量
     */
    /**public void setUnreadCount(int num) {
     if (num <= 0) {
     mTvUnreadCount.setText(String.valueOf(0));
     mTvUnreadCount.setVisibility(GONE);
     } else {
     mTvUnreadCount.setVisibility(VISIBLE);
     if (num > 99) {
     mTvUnreadCount.setText("99+");
     } else {
     mTvUnreadCount.setText(String.valueOf(num));
     }
     }
     }*/

    /**
     * 获取当前未读数量
     */
    /**
     * public int getUnreadCount() {
     * int count = 0;
     * if (TextUtils.isEmpty(mTvUnreadCount.getText())) {
     * return count;
     * }
     * if (mTvUnreadCount.getText().toString().equals("99+")) {
     * return 99;
     * }
     * try {
     * count = Integer.valueOf(mTvUnreadCount.getText().toString());
     * } catch (Exception ignored) {
     * }
     * return count;
     * }
     */
    private int dip2px(Context context, float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }
}
